﻿namespace ThreeByte.ActiveDeck.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using System.Web.Profile;
    using System.Web.Security;
    using ThreeByte.ActiveDeck.Web.Resources;
    using System.Web;
    using log4net;
    using ThreeByte.ActiveDeck.Data;
    using System.Web.Configuration;
    using ThreeByte.ActiveDeck.Service;
    using ThreeByte.ActiveDeck.Web.Data;

    // TODO: Switch to a secure endpoint when deploying the application.
    //       The user's name and password should only be passed using https.
    //       To do this, set the RequiresSecureEndpoint property on EnableClientAccessAttribute to true.
    //   
    [EnableClientAccess(RequiresSecureEndpoint = true)]
    //
    //       More information on using https with a Domain Service can be found on MSDN.

    /// <summary>
    /// Domain Service responsible for registering users.
    /// </summary>
    //[EnableClientAccess]
    [RequiresAuthentication()]
    public class DashboardAccessService : DomainService
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(DashboardAccessService));

        
        [Query(IsComposable = false)]
        public DownloadInfo GetDownloadVersion() {
            DownloadInfo info = new DownloadInfo();
            info.Version = DataUtil.GetConfigValue(@"AddInVersion");
            info.LastUpdated = DataUtil.GetConfigValue(@"AddInUpdated");
            return info;
        }

        [Query(IsComposable = false)]
        public DownloadInfo GetBetaDownloadVersion() {
            DownloadInfo info = new DownloadInfo();
            info.Version = DataUtil.GetConfigValue(@"AddInVersion_Beta");
            info.LastUpdated = DataUtil.GetConfigValue(@"AddInUpdated_Beta");
            return info;
        }

        public bool PromoteBetaDownloadVersion() {
            DownloadInfo info = new DownloadInfo();
            info.Version = DataUtil.GetConfigValue(@"AddInVersion_Beta");
            info.LastUpdated = DataUtil.GetConfigValue(@"AddInUpdated_Beta");
            
            string sourceName = string.Format("installers/beta/{0}/ActiveDeck Setup.exe", info.Version);
            string destName = string.Format("installers/{0}/ActiveDeck Setup.exe", info.Version);

            //Initiate the copy operation
            try {
                CloudStorageUtil.CopyDownload(sourceName, destName);
            } catch(Exception) {
                return false;
            }

            //Update the new production values
            DataUtil.SetConfigValue(@"AddInVersion", info.Version);
            DataUtil.SetConfigValue(@"AddInUpdated", info.LastUpdated);

            return true;
        }

        public string GetDownloadLink() {
            string username = HttpContext.Current.User.Identity.Name;
            Guid linkGuid = DataUtil.RecordDownloadRequest(username);
            return EmailUtil.GetDownloadLink(linkGuid);
        }


        public void SendEmailVerification() {
            //string username = HttpContext.Current.User.Identity.Name;
            MembershipUser user = Membership.GetUser();
            Guid emailKey = DataUtil.RecordEmailChallenge(user.Email);

            EmailUtil.SendVerificationEmail(user.Email, emailKey);
        }

        public bool IsEmailVerified() {
            MembershipUser user = Membership.GetUser();
            return DataUtil.IsEmailVerified(user.Email);
        }

        public bool IsAuthorizedPresenter(string presentationId) {
            string username = HttpContext.Current.User.Identity.Name;
            return AuthorizationUtil.IsAuthorizedPresenter(presentationId, username); 
        }
    }

    public sealed class DownloadInfo
    {
        [Key]
        public string Version { get; set; }
        public string LastUpdated { get; set; }
    }



}